package com.whu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whu.aircraft.PictureInfo;
import com.whu.dao.interfaces.PicInfoDao;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PicInfoService {
    final PicInfoDao picInfoDao;
    final XmlRpcClient xmlRpcClient;

    public PicInfoService(PicInfoDao picInfoDao, XmlRpcClient xmlRpcClient) throws MalformedURLException {
        this.picInfoDao = picInfoDao;
        this.xmlRpcClient = xmlRpcClient;
    }
    public PictureInfo selectById(long id){
        return picInfoDao.selectById(id);
    }
    public void insertPicInfo(PictureInfo pictureInfo){
        picInfoDao.insert(pictureInfo);
    }
    public List<PictureInfo> selectByTimeAndPos(PictureInfo pictureInfo){
        QueryWrapper<PictureInfo> queryWrapper =new QueryWrapper<>();
        //7天内的图片
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date_now = pictureInfo.getPicCreateTime();

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date_now);
        calendar.add(Calendar.DATE,-7);
        Date date_past_day = calendar.getTime();

        Calendar start_calendar = new GregorianCalendar();
        start_calendar.setTime(date_now);
        start_calendar.add(Calendar.DATE,-1);
        Date date_start_day = start_calendar.getTime();

        String date_now_str = dateFormat.format(date_start_day);
        String date_past_str = dateFormat.format(date_past_day);

        double cur_longitude = pictureInfo.getLongitude();
        double cur_latitude = pictureInfo.getLatitude();
        double l_longitude = cur_longitude-0.00003,r_longitude = cur_longitude+0.00003;
        double l_latitude = cur_latitude-0.00003,r_latitude = cur_latitude+0.00003;

        queryWrapper.eq("detect",0)
                .between("pic_create_time",date_past_str,date_now_str)
                .between("longitude",l_longitude,r_longitude)
                .between("latitude",l_latitude,r_latitude)
                .orderByDesc("pic_create_time")
                .last("limit 0,6");

        return picInfoDao.selectList(queryWrapper);
    }
    //return path or "noIllegalBuilding"
    public String imageMatchRpc(long id){
        PictureInfo pictureInfo = selectById(id);
        return imageMatchRpc(pictureInfo);
    }
    public String imageMatchRpc(PictureInfo pictureInfo){
        List<PictureInfo> pics = selectByTimeAndPos(pictureInfo);
        List<String> paths = new ArrayList<>();
        paths.add(pictureInfo.getPicPath());
        if(pics.isEmpty()){
            return "noIllegalBuilding";
        }else{
            for(PictureInfo pic:pics)
                paths.add(pic.getPicPath());
            Object[] params = new Object[]{paths.toArray()};
            try{
                if(xmlRpcClient==null)System.out.println("clientRPC空");
                assert xmlRpcClient != null;
                return (String) xmlRpcClient.execute("illegalBuildingObjective",params);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return "noIllegalBuilding";
    }

    public void testRpcPython() throws MalformedURLException {
        List<String> path = new ArrayList<>();
        path.add("484");path.add("788");
        Object[] params1 = new Object[]{path.toArray()};
        try{
            System.out.println(xmlRpcClient.execute("add",params1));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
